package com.imooc.xunwu.base;

import com.imooc.xunwu.entity.User;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @Auther: hyr
 * @Date: 2018/10/25 14:46
 * @Description:
 */
public class LoginUserUtil {
    public static User load(){
        Object principal =SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal != null && principal instanceof User){
            return (User) principal;
        }
        return null;
    }

    public static Long getLoginUserId(){
        User user = load();
        if (user == null){
            return -1L;
        }
        return user.getId();
    }
}
